package com.example.my_music_palyer.common.tools;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * 图片上传工具类
 */
@Component
public class ImageTools {

    @Value("${image.local}")
    private String url;

    public int upload(MultipartFile file, int userId) throws IOException {
        try {
            if (file == null || file.isEmpty()) {
                System.out.println("文件为空，无法上传");
                return 0;
            }

            String fileName = file.getOriginalFilename();//获取文件名
            if (fileName == null || fileName.trim().isEmpty()) {
                System.out.println("文件名为空，无法上传");
                return 0;
            }

            // 确保路径存在
            File saveDir = new File(url);
            if (!saveDir.exists()) {
                boolean created = saveDir.mkdirs();
                if (!created) {
                    System.out.println("无法创建目录: " + url);
                    return 0;
                }
                System.out.println("创建目录成功: " + url);
            }

            String savePath = url + fileName;
            File saveFile = new File(savePath);
            
            System.out.println("存储地址: " + savePath);
            System.out.println("访问地址: " + fileName);

            // 如果文件已存在，先删除
            if (saveFile.exists()) {
                boolean deleted = saveFile.delete();
                System.out.println("删除已存在的文件: " + deleted);
            }

            // 保存文件
            file.transferTo(saveFile);
            
            // 验证文件是否保存成功
            if (saveFile.exists() && saveFile.length() > 0) {
                System.out.println("文件上传成功: " + savePath + ", 大小: " + saveFile.length() + " bytes");
                return 1;
            } else {
                System.out.println("文件上传失败，文件不存在或大小为0");
                return 0;
            }
            
        } catch (Exception e) {
            System.out.println("文件上传过程中发生错误: " + e.getMessage());
            e.printStackTrace();
            return 0;
        }
    }

    public int delete(String fileName) {
        try {
            if (fileName == null || fileName.trim().isEmpty()) {
                System.out.println("文件名为空，无法删除");
                return 0;
            }

            String fileUrl = url + fileName;
            File file = new File(fileUrl);
            
            if (!file.exists()) {
                System.out.println("文件不存在: " + fileUrl);
                return 1; // 文件不存在也算删除成功
            }
            
            boolean deleted = file.delete();
            if (deleted) {
                System.out.println("文件删除成功: " + fileUrl);
                return 1;
            } else {
                System.out.println("文件删除失败: " + fileUrl);
                return 0;
            }
            
        } catch (Exception e) {
            System.out.println("文件删除过程中发生错误: " + e.getMessage());
            e.printStackTrace();
            return 0;
        }
    }
}
